﻿// Models/ExamDbInitializer.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using WebApplication1.Models;

namespace WebApplication1.Models
{
    public class ExamDbInitializer : DropCreateDatabaseIfModelChanges<ExamDbContext>
    {
        protected override void Seed(ExamDbContext context)
        {
            // 初始化管理员用户
            var adminUser = new User
            {
                Username = "admin",
                Password = "admin123", // 实际项目中应加密存储
                FullName = "系统管理员",
                Role = "Admin",
                Department = "IT部"
            };

            var employeeUser = new User
            {
                Username = "employee",
                Password = "employee123", // 实际项目中应加密存储
                FullName = "普通员工",
                Role = "Employee",
                Department = "人事部"
            };

            context.Users.Add(adminUser);
            context.Users.Add(employeeUser);
            context.SaveChanges(); // 先保存用户以获取UserId

            // 初始化示例考试
            var sampleExam = new Exam
            {
                ExamName = "入职培训考试",
                Description = "新员工入职培训考试",
                CreatedDate = DateTime.Now,
                IsActive = true,
                Duration = 60
            };

            context.Exams.Add(sampleExam);
            context.SaveChanges(); // 先保存考试以获取ExamId

            // 初始化示例试题
            var question1 = new Question
            {
                ExamId = sampleExam.ExamId,
                QuestionText = "公司的成立时间是？",
                QuestionType = "Single",
                Score = 10,
                CorrectAnswer = "A"
            };

            var question2 = new Question
            {
                ExamId = sampleExam.ExamId,
                QuestionText = "以下哪些是公司的核心价值观？",
                QuestionType = "Multiple",
                Score = 20,
                CorrectAnswer = "A,B,D"
            };

            var question3 = new Question
            {
                ExamId = sampleExam.ExamId,
                QuestionText = "公司总部位于北京。",
                QuestionType = "TrueFalse",
                Score = 10,
                CorrectAnswer = "True"
            };

            context.Questions.Add(question1);
            context.Questions.Add(question2);
            context.Questions.Add(question3);
            context.SaveChanges(); // 保存试题以获取QuestionId

            // 初始化选项
            var options1 = new List<Option>
            {
                new Option { QuestionId = question1.QuestionId, OptionText = "2000年", OptionLabel = "A" },
                new Option { QuestionId = question1.QuestionId, OptionText = "2005年", OptionLabel = "B" },
                new Option { QuestionId = question1.QuestionId, OptionText = "2010年", OptionLabel = "C" },
                new Option { QuestionId = question1.QuestionId, OptionText = "2015年", OptionLabel = "D" }
            };

            var options2 = new List<Option>
            {
                new Option { QuestionId = question2.QuestionId, OptionText = "客户至上", OptionLabel = "A" },
                new Option { QuestionId = question2.QuestionId, OptionText = "团队合作", OptionLabel = "B" },
                new Option { QuestionId = question2.QuestionId, OptionText = "追求利润", OptionLabel = "C" },
                new Option { QuestionId = question2.QuestionId, OptionText = "持续创新", OptionLabel = "D" }
            };

            var options3 = new List<Option>
            {
                new Option { QuestionId = question3.QuestionId, OptionText = "正确", OptionLabel = "True" },
                new Option { QuestionId = question3.QuestionId, OptionText = "错误", OptionLabel = "False" }
            };

            context.Options.AddRange(options1);
            context.Options.AddRange(options2);
            context.Options.AddRange(options3);
            context.SaveChanges();

            base.Seed(context);
        }
    }
}